package com.base.components.common.util;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Pattern;

/**
 * @author <a href="tecyun@foxmail.com">Huangyunyang</a>
 * @version 1.0.0, 2018/6/15 0015 11:56
 */
public class BrowserUtil {
  /**
   * \b 是单词边界(连着的两个(字母字符 与 非字母字符) 之间的逻辑上的间隔),
   * 字符串在编译时会被转码一次,所以是 "\\b"
   * \B 是单词内部逻辑间隔(连着的两个字母字符之间的逻辑上的间隔)
   */
  private static final String phoneReg = "\\b(ip(hone|od)|android|opera m(ob|in)i"
    +"|windows (phone|ce)|blackberry"
    +"|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp"
    +"|laystation portable)|nokia|fennec|htc[-_]"
    +"|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";

  private static final String tabletReg = "\\b(ipad|tablet|(Nexus 7)|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";

  /**
   * 移动设备正则匹配：手机端、平板
   */
  private static Pattern phonePat = Pattern.compile(phoneReg, Pattern.CASE_INSENSITIVE);
  private static Pattern tabletPat = Pattern.compile(tabletReg, Pattern.CASE_INSENSITIVE);

  /**
   * 检测是否是移动设备访问
   *
   * @param request
   * @return true:移动设备接入，false:pc端接入
   */
  public static Boolean isMobile(HttpServletRequest request){
    try{
      String userAgent = request.getHeader("user-Agent");
      return phonePat.matcher(userAgent).find() || tabletPat.matcher(userAgent).find();
    } catch (Exception e){
    }
    return false;
  }
}
